function formatDecimal(obj,decPlace) {
var objval = obj.value;
var objval = obj.value;
    switch (decPlace) {
            case 2: dec = 100; break;
            case 3: dec = 1000; break;
            case 5: dec=100000; break;
            default: dec = 10;
    }
    numLen = objval.length;
    num = objval.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*dec+0.50000000001);
	cents=num%dec;
	if ((cents+"").length < decPlace) {
		var zeros = "";
		for (var i=0; i < (decPlace-(cents+"").length); i++) {
			zeros += "0";
		}
		cents = zeros + cents;
	}
    num = Math.floor(num/dec).toString();
    if ((num + '.' + cents).length < numLen) {
        alert('Amount rounded off to ' + decPlace + ' decimal places!');
    }
    obj.value = (((sign)?'':'-') + num + '.' + cents);
}

function formatDecimalPlace(obj,decPlace) {
var objval = obj.value;
    switch (decPlace) {
            case 2: dec = 100; break;
            case 3: dec = 1000; break;
            case 5: dec=100000; break;
            default: dec = 10;
    }
    numLen = objval.length;
    num = objval.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*dec+0.50000000001);
	cents=num%dec;
	if ((cents+"").length < decPlace) {
		var zeros = "";
		for (var i=0; i < (decPlace-(cents+"").length); i++) {
			zeros += "0";
		}
		cents = zeros + cents;
	}
    num = Math.floor(num/dec).toString();

    obj.value = (((sign)?'':'-') + num + '.' + cents);
}


function formatUKCurrency(pNumber, pDollarSign, pNumDecPlace) {
	pNumber = unformatNumber(pNumber.toString());

	if (pDollarSign == null) {
		pDollarSign = true;
	}

	pNumber = setDecimalPlace(pNumber, pNumDecPlace);

	var _numArray = pNumber.toString().split('.');
	var _negSign = '';
	if (_numArray[0].charAt(0) == '-') {
		_negSign = '-'
		_numArray[0] = _numArray[0].substring(1);
	}

	for (var i = 0; i < Math.floor((_numArray[0].length-(1+i))/3); i++) {
		_numArray[0] = _numArray[0].substring(0,_numArray[0].length-(4*i+3)) + ' ' +
					   _numArray[0].substring(_numArray[0].length-(4*i+3));
	}

	return (_negSign +
			(pDollarSign == true ? '$' : '') +
			_numArray[0] +
			(pNumDecPlace == 0 ? '' : '.' + _numArray[1]));
}

function formatCurrency(pNumber, pDollarSign, pNumDecPlace) {
	pNumber = unformatNumber(pNumber.toString());

	if (pDollarSign == null) {
		pDollarSign = true;
	}

	pNumber = setDecimalPlace(pNumber, pNumDecPlace);

	var _numArray = pNumber.toString().split('.');
	var _negSign = '';
	if (_numArray[0].charAt(0) == '-') {
		_negSign = '-'
		_numArray[0] = _numArray[0].substring(1);
	}

	for (var i = 0; i < Math.floor((_numArray[0].length-(1+i))/3); i++) {
		_numArray[0] = _numArray[0].substring(0,_numArray[0].length-(4*i+3)) + ',' +
					   _numArray[0].substring(_numArray[0].length-(4*i+3));
	}

	return (_negSign +
			(pDollarSign == true ? '$' : '') +
			_numArray[0] +
			(pNumDecPlace == 0 ? '' : '.' + _numArray[1]));
}

function formatCode(pNumber) {
	var _numArray = pNumber.toString().split('.');
	for (var i = 0; i < Math.floor((_numArray[0].length-(1+i))/3); i++) {
		_numArray[0] = _numArray[0].substring(0,_numArray[0].length-(4*i+3)) + '.' +
					   _numArray[0].substring(_numArray[0].length-(4*i+3));
	}

	return ( _numArray[0] );
}


function formatNumber(pNumberobj, pDecimalPlace, pSeparator) {

	pNumber = unformatNumber(removeNumericSep(pNumberobj.value.toString()));

	if (pSeparator == null) {
		pSeparator = false;
	}

	pNumber = setDecimalPlace(pNumber, pDecimalPlace);

	var _numArray = pNumber.toString().split('.');
	var _negSign = '';
	if (_numArray[0].charAt(0) == '-') {
		_negSign = '-'
		_numArray[0] = _numArray[0].substring(1);
	}

	if (pSeparator == true) {
		for (var i = 0; i < Math.floor((_numArray[0].length-(1+i))/3); i++) {
			_numArray[0] = _numArray[0].substring(0,_numArray[0].length-(4*i+3)) + ' ' +
			               _numArray[0].substring(_numArray[0].length-(4*i+3));
		}
	}
	pNumberobj.value = (_negSign + _numArray[0] + (pDecimalPlace == 0 ? '' : '.' + _numArray[1]));
	return (_negSign + _numArray[0] + (pDecimalPlace == 0 ? '' : '.' + _numArray[1]));
}

function checkMaxValue(obj,pWholeNum,pDecimal){
	var checkVal = 0;
	var wholeVal = 0;
	var decimalVal = 0;
	var objval = obj.value;
	var numVal = 0;
	var result = 0;
	var ctr3 = 1;
	for (var ctr=0; ctr<pWholeNum; ctr++){
		numVal = 9 * Math.pow(10,ctr);
		wholeVal = wholeVal + numVal;
	}
	for (var ctr=1; ctr<=pDecimal; ctr++){
		numVal = 9 / Math.pow(10,ctr);
		decimalVal = decimalVal + numVal;
	}
	checkVal = wholeVal + decimalVal;
	//checkVal = Math.round(checkVal);
	result = objval - checkVal;
	//checkVal = formatUKCurrency(checkVal,' ',pDecimal);
	wholeVal = "";
	decimalVal = "";
	for (var ctr=0; ctr<pWholeNum; ctr++){
		if(ctr3 == 3){
			wholeVal = ' 9' + wholeVal;
			ctr3 = 1;
		}else{
			wholeVal = '9' + wholeVal;
			ctr3++;
		}
	}
	for (var ctr=1; ctr<=pDecimal; ctr++){
		decimalVal = decimalVal + '9';
	}
	checkVal = wholeVal + '.' + decimalVal;
	if (result > 0){
		//return false;
		alert('Value cannot be greater than '+ checkVal);
		obj.value="";
	}else{
		//return true;
                formatUKCurrency(obj,'', pDecimal);
		//formatNumber(obj,pDecimal,'.');
	}
}

function checkMaxValuePrcnt(obj,pWholeNum,pDecimal){
	var checkVal = 0;
	var wholeVal = 0;
	var decimalVal = 0;
	var objval = obj.value;
	var numVal = 0;
	var result = 0;
	for (var ctr=0; ctr<pWholeNum; ctr++){
		numVal = 9 * Math.pow(10,ctr);
		wholeVal = wholeVal + numVal;
	}
	for (var ctr=1; ctr<=pDecimal; ctr++){
		
		numVal = 9 / Math.pow(10,ctr);
		decimalVal = decimalVal + numVal;
	}
	checkVal = wholeVal + decimalVal;
	checkVal = Math.round(checkVal);
	result = objval - checkVal;
	checkVal = formatUKCurrency(checkVal,' ',pDecimal);
	if (result > 0){
		//return false;
		alert('Value cannot be greater than '+checkVal);
		obj.value="";
	}else{
		//return true;
                formatUKCurrency(obj,'', pDecimal);
		//formatNumber(obj,pDecimal,'.');
	}
}

function checkMaxValueAlcohol(obj,pWholeNum,pDecimal){
	var checkVal = 0;
	var wholeVal = 0;
	var decimalVal = 0;
	var objval = obj.value;
	var numVal = 0;
	var result = 0;
	for (var ctr=0; ctr<pWholeNum; ctr++){
		numVal = 9 * Math.pow(10,ctr);
		wholeVal = wholeVal + numVal;
	}
	for (var ctr=1; ctr<=pDecimal; ctr++){
		
		numVal = 9 / Math.pow(10,ctr);
		decimalVal = decimalVal + numVal;
	}
	checkVal = wholeVal + decimalVal;
	result = objval - checkVal;
	checkVal = formatUKCurrency(checkVal,' ',pDecimal);
	if (result > 0){
		//return false;
		alert('Value cannot be greater than '+checkVal);
		obj.value="";
	}else{
		//return true;
                formatUKCurrency(obj,'', pDecimal);
		//formatNumber(obj,pDecimal,'.');
	}
}

function zerofy(pInputVal) {
	return (parseFloat(pInputVal) == 0)? 0 : pInputVal;
}

function removeNumericSep(pInput,sepChar){
var _pInput = '';
if (pInput.length  < 1) {
return pInput
}
if (sepChar == null) {
	sepChar = ' ';
}
for (var i = 0; i < pInput.length; i++) {
		if(pInput.charAt(i) != sepChar) {
			_pInput +=   pInput.charAt(i);
		};

}

return _pInput
}
function isInteger(pInputVal) {
	var _inputStr = pInputVal.toString();
	var _char;

	if (_inputStr == "-" || _inputStr == ".") {
		return false;
	}

	for(var i = 0; i < _inputStr.length; i++) {
		_char = _inputStr.charAt(i);
		if(i == 0 && _char == "-") {
			continue;
		}
		if (_char == ".") {
			continue;
		}
		if (_char < "0" || _char > "9") {
			return false;
		}
	}
	return true;
}


function isNumber(pInputVal) {
	var _inputStr = removeNumericSep(pInputVal.value.toString())

	var _char;
	var _cntDec =  0 ;
	var _cntNeg =  0 ;
	var decimalPos = _inputStr.indexOf(".");

	if (isNaN(_inputStr)) {
			pInputVal.select();
                alert("Please enter numeric values for this field");
		pInputVal.focus();
		return false;
	}

	for (var i = 0; i < _inputStr.length; i++) {
		_char = _inputStr.charAt(i);

		if (_char == "-" || _char == "+" || _char == ".") {
			continue;
		}

		if (_char < "0" || _char > "9") {
		 		pInputVal.select();
                        alert("Please enter numeric values for this field");
			pInputVal.focus();
			return false;
		}
	}
	return true;
}


function isNonnegativeNumber(pInputVal) {
	var _inputStr = pInputVal.toString();
	var _char;
	var _cnt =  0 ;

	if (isNaN(pInputVal)) {
		return false;
	}

	for (var i = 0; i < _inputStr.length; i++) {
		_char = _inputStr.charAt(i);

		if (_char == "." || _char == "+") {
			continue;
		}

		if (_char < "0" || _char > "9") {
			return false;
		}
	}
	return true;
}

function isICQNumber(pInputVal) {

	if (isNaN(pInputVal)) {
		return false;
	}

	return true;
}

function isPositiveNumber(pInputVal) {
	var _inputStr = pInputVal.toString();
	var _char;
	var _cnt =  0 ;

	if (isNaN(pInputVal)) {

		return false;
	}

	if (Number(pInputVal) <= 0) {
		return false;
	}

	for (var i = 0; i < _inputStr.length; i++) {
		_char = _inputStr.charAt(i);

		if (_char == "." || _char == "+") {
			continue;
		}

		if (_char < "0" || _char > "9") {
			return false;
		}
	}
	return true;
}


function isNegativeInteger(pInputVal) {
	if (isNaN(pInputVal)) {
		return false;
	}
	if (Number(pInputVal) >= 0) {
		return false;
	}
	return isInteger(pInputVal);
}

// internal Used
function isNonnegativeInteger(pInputVal) {
	var _inputStr = pInputVal.toString();
	var _char;
	for (var i = 0; i < _inputStr.length; i++) {
		_char = _inputStr.charAt(i);
		if (_char < "0" || _char > "9") {
			return false;
		}
	}
	return true;
}

function isCorrectDecimal(pInputVal, pDecimalPlace) {
	var _inputStr = pInputVal.toString();
	var decimalSeparator = ".";
	var decimalPos = _inputStr.indexOf(decimalSeparator);


	if (decimalPos != -1)  {
		if (pDecimalPlace == null || !isNonnegativeInteger(pDecimalPlace)) {
			pDecimalPlace =  2 ;
		}
		if (_inputStr.length > (decimalPos + Number(pDecimalPlace) + 1)) {
			return false;
		}
	}

	return true;
}


function isPositiveInteger(pInputVal) {
	if (isNaN(pInputVal)) {
		return false;
	}
	if (Number(pInputVal) <= 0) {
		return false;
	}
	return isNonnegativeInteger(pInputVal);
}

/* Internal Used */
function setDecimalPlace(pNumber, pDecimalPlace) {
	if (isNaN(pNumber)) {
		pNumber =  0 ;
	}

	if (pDecimalPlace == null || !isNonnegativeInteger(pDecimalPlace)) {
		pDecimalPlace =  2 ;
	}

	if (pDecimalPlace > 0) {
		pNumber = Math.round(pNumber*Math.pow(10, pDecimalPlace))/Math.pow(10, pDecimalPlace);
	   	var _numArray = pNumber.toString().split('.');
		if (_numArray.length == 1) {
			_numArray[1] = '';
		}

		var _numLoop = pDecimalPlace - _numArray[1].length;
		for (var i = 0; i < _numLoop; i++) {
			_numArray[1] = _numArray[1] + '0';
		}

		return _numArray[0] + '.' + _numArray[1];
	}
	else {
		return Math.round(pNumber);
	}
}

/* internal Used */
function unformatNumber(pInputVal) {
	var _regExp = /\$|,/g;
	_temp = (pInputVal.toString()).replace(_regExp, '');
	return (isNaN(_temp) ? pInputVal : _temp);
}


/* 0-9 and (.) only input */
function trapNumeric(){
        event.returnValue = ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46))? true : false;
}

/* 0-9 and (.) only input */
function trapDotNumeric(obj){
	if(event.keyCode == 46 && obj.value.indexOf('.') > 0){
		event.returnValue = false;
		return false;
	}
    event.returnValue = ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46))? true : false;
}


/* 0-9 ('.' and '-') only input */
function trapNumNegative(obj){
	if(event.keyCode == 45 && obj.value.search(/\-/g) != -1){
		event.returnValue = false;
		return false;
	}
        event.returnValue = ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46) || (event.keyCode == 45))? true : false;
}

function trapDashStart(obj){
	if(obj.value.indexOf('-') > 0){
		obj.value = obj.value.replace('-','');
	}
}


/* 0-9 only input */
function trapInteger(){
        event.returnValue = (event.keyCode >= 48 && event.keyCode <= 57)? true : false;
}

/* a-z,A-Z only input */
function trapAlphaChars()
{
	alert('test');
    event.returnValue = ((event.keyCode >= 65) && (event.keyCode <= 90) ||
                         (event.keyCode >= 97) && (event.keyCode <= 122))?true:false;
}

/* a-z,A-Z and limited ASCII characters only input */
function trapAlphaNumAsciiChars(){
     	    
     
	bOK = ((event.keyCode >= 32) && (event.keyCode <= 57) || (event.keyCode >= 63) && (event.keyCode <= 90) ||
                         (event.keyCode >= 97) && (event.keyCode <= 122) || (event.keyCode >= 192) && (event.keyCode <= 220) ||
				 (event.keyCode >= 224) && (event.keyCode <= 252) || (event.keyCode == 171) || (event.keyCode == 187) || (event.keyCode == 13))?true:false;
 
     if(!bOK)
	alert("Invalid Character");
	
	
	event.returnValue = bOK;
	
}


/* a-z,A-Z,0-9 only input*/
function trapAlphaNumericChars()
{
	
    event.returnValue = ((event.keyCode >= 65) && (event.keyCode <= 90) ||
                         (event.keyCode >= 97) && (event.keyCode <= 122)||
                         (event.keyCode >= 48 && event.keyCode <= 57))?true:false;
}

/* trap colon char*/
function trapColon(){
    event.returnValue = (event.keyCode != 58)? true : false;
}

function trapApostrophe(){
     event.returnValue = (event.keyCode != 39)? true : false;
}

function trapPhoneNumber(){
        event.returnValue = ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46) ||
                             (event.keyCode == 32) || (event.keyCode == 40) || (event.keyCode == 41) ||
                             (event.keyCode == 45) || (event.keyCode == 43))? true : false;
}

function trapAlphaInNum(obj,objFld){
	var regexp = /[a-zA-Z]/;
		checktest = obj.value.match(regexp);
	if(checktest!= null){
		alert(objFld + ' number should not contain alpha characters.');
		obj.value = "";
	}	
}

function trapNumericOnPaste(obj){
	var regexp = /[^0-9.]/;
	checktest = obj.value.match(regexp);
	if(checktest!= null){
		alert('Only numeric characters are allowed!');
		obj.value = "";
	}
}

function trapIntegerOnPaste(obj,defVal){
	var regexp = /[^0-9]/;
	checktest = obj.value.match(regexp);
	if(checktest!= null){
		alert('Only numeric characters are allowed!');
		if(defVal!=null){
			obj.value = defVal;
		}else{
			obj.value = "";
		}
	}
}

function limitNumericAlphaOnPaste(obj,numeric,alpha){
    var limit = numeric + alpha;
    //alert(Event.keyCode);
    if((event.keyCode != 8)&&(event.keyCode != 46)){ //trap delete and backspace
    if (obj.value.length < limit){
        if (obj.value.length <= numeric) {
            	var regexp = /[^0-9]/;
		checktest = obj.value.match(regexp);
		if((checktest != null)||(obj.value.length == limit)){
			alert('Invalid Format');
			return false;
		}
	}
    } else {
            	var regexp = /\d{12}[a-zA-Z]/;
		checktest = obj.value.match(regexp);
		if(checktest == null){
			alert('Invalid Format');
			return false;
		}
    }
    } 
return true;
}

function trapColonOnPaste(obj){
    objtxtval = obj.value; 
   // alert(objtxtval.indexOf(":"));
    if (objtxtval.indexOf(":") > -1) { 
    
    objtxtval = objtxtval.replace(/:/g," ");
    obj.value = objtxtval;
    }
}

function traptrapApostropheOnPaste(obj){
	objtxtval = obj.value; 
    if (objtxtval.indexOf("'") > -1) { 
    
    objtxtval = objtxtval.replace(/\'/g," ");
    obj.value = objtxtval;
    }

}